package com.jason.controller;

import com.jason.model.entity.VideoOrder;
import com.jason.model.request.VideoOrderRequest;
import com.jason.service.VideoOrderServce;
import com.jason.utlis.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/v1/pri/order")
public class VideoOrderController {
    @Autowired
    private VideoOrderServce videoOrderServce;
    /**
     * 下单接口
     * @return
     */
    @RequestMapping("/save")
    public JsonData saveOrder(@RequestBody VideoOrderRequest videoOrderRequest, HttpServletRequest request){

        Integer userId=(Integer) request.getAttribute("user_id");
        int rows = videoOrderServce.save(userId, videoOrderRequest.getVideoId());

        return rows==0?JsonData.bulidError("下单失败"):JsonData.buildSuccess();

    }

    /**
     * 订单列表
     * @param request
     * @return
     */
    @GetMapping("/list")
    public JsonData listOrder(HttpServletRequest request){
        Integer userId=(Integer) request.getAttribute("user_id");
        List<VideoOrder> videoOrderList=videoOrderServce.listOrderByUserId(userId);
        return JsonData.buildSuccess(videoOrderList);
    }
}
